package com.wogu.jwa.web.admin.model;

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.wogu.jwa.classes.Model;

public class AdminModel extends Model {
	
	public AdminModel() {
		super("admin");
	}
	
	public int addAdmin(Map<String, String> map) throws SQLException {
		return insert(map, true);
	}
	
	public ResultSet getAdmin(String fields, String where, String[] whereData, int resultSetConcurrency) throws SQLException {
		ResultSet rs = select(fields, where, whereData, null, "0,1", 0, resultSetConcurrency);
		rs.next();
		return rs;
	}
	
	public ResultSet getAdmin(String fields, String where, String[] whereData) throws SQLException {
		return getAdmin(fields, where, whereData, 0);
	}
	
	public ResultSet getAdmins(String fields, String where, String[] whereData, String order, String limit) throws SQLException {
		return select(fields, where, whereData, order, limit);
	}
	
	public int deleteAdmin(String where, String[] whereData) throws SQLException {
		return delete(where, whereData);
	}
	
	public int updateAdmin(HashMap<String, String> data, String where, String[] whereData) throws SQLException {
		return update(data, where, whereData);
	}
	
	public Map<String, Object> list(int page, int perPage, String fields, String where, String[] whereData, String order, String urlRule) throws SQLException {
		Map<String, Object> data = super.list(page, perPage, fields, where, whereData, order, urlRule);
		if(null == data) return null;
		final ResultSet rs = (ResultSet) data.get("items");
		ArrayList<HashMap<String, String>> itemsList = new ArrayList<>();
		while(rs.next()) {
			itemsList.add(new HashMap<String, String>(){{
				put("userid", rs.getString("userid"));
				put("groupid", rs.getString("groupid"));
				put("username", rs.getString("username"));
				put("password", rs.getString("password"));
				put("qq_openid", rs.getString("qq_openid"));
				put("salt", rs.getString("salt"));
				put("force_qq", rs.getString("force_qq"));
				put("isfree", rs.getString("isfree"));
				put("isstop", rs.getString("isstop"));
				put("ip", rs.getString("ip"));
				put("lastvist", rs.getString("lastvist"));
			}});
		}
		
		data.put("itemsList", itemsList);
		rs.close();
		return data;
	}
}